
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<%-- 导入面包屑 --%>
<jsp:include page="../common/nav.jsp"></jsp:include>


<div class="wrapper wrapper-content">

	<%-- 导入提示信息框 --%>
    <c:if test="${hint!=null&&hint!=''}">
   		<jsp:include page="../common/alert/alert.jsp">
   			<jsp:param value="${hint}" name="alertType"/>
   			<jsp:param value="${alertMessage}" name="alertMessage"/>
   		</jsp:include>
    </c:if>

	<div class="row">
		<div class="col-lg-3">
			<div class="ibox ">
				<div class="ibox-content mailbox-content">
					<div class="file-manager">
						<select class="form-control" name="periodId" id="periodId">
                        	<option value="" selected="selected">请选择结算期间...</option>
                            <c:forEach items="${requestScope.calendarPeriodList}" var="calendarPeriod">
                        		<option value="${calendarPeriod.periodId}">${calendarPeriod.periodName}</option>
                        	</c:forEach>
                        </select><br/>
					
						<button id="validateButton" class="ladda-button ladda-button-demo btn btn-block btn-primary btn-lg" data-style="expand-right">结算前验证</button>	
							
						<div class="space-25"></div>
						<h5>验证步骤：</h5>
						<ul class="folder-list m-b-md" style="padding: 0">
							<li><a href="javascript:void(0)"> <i class="fa fa-circle step1"></i>
									期间验证 <span class="label float-right step1Text">未验证</span>
							</a></li>
							<li><a href="javascript:void(0)"> <i class="fa fa-circle step2"></i>
									子分类账验证 <span class="label float-right step2Text">未验证</span>
							</a></li>
							<li><a href="javascript:void(0)"> <i class="fa fa-circle step3"></i>
									日记账验证 <span class="label float-right step3Text">未验证</span>
							</a></li>
							<li><a href="javascript:void(0)"> <i class="fa fa-circle step4"></i>
									试算平衡验证 <span class="label float-right step4Text">未验证</span>
							</a></li>
							<li><a href="javascript:void(0)"> <i class="fa fa-circle step5"></i>
									对账验证 <span class="label float-right step5Text">未验证</span></a></li>
						</ul>
						<h5>图例</h5>
						<ul class="category-list" style="padding: 0">
							<li><a href="#"> <i class="fa fa-circle text-info"></i>
									验证通过，无任何问题
							</a></li>
							<li><a href="#"> <i class="fa fa-circle text-danger"></i>
									验证不通过，必须解决，否则无法结账
							</a></li>
							<li><a href="#"> <i class="fa fa-circle text-warning"></i>
									验证警告，建议解决，不影响结账
							</a></li>
						</ul>

						<h5 class="tag-title">快速导航</h5>
						<ul class="tag-list" style="padding: 0">
							<li><a href="javascript:void(0)"><i class="fa fa-link"></i> 子分类账</a></li>
							<li><a href="javascript:void(0)"><i class="fa fa-link"></i> 日记账</a></li>
							<li><a href="javascript:void(0)"><i class="fa fa-link"></i> 期间管理</a></li>
							<li><a href="javascript:void(0)"><i class="fa fa-bar-chart"></i> 科目余额表</a></li>
							<li><a href="javascript:void(0)"><i class="fa fa-bar-chart"></i> 试算平衡表</a></li>
							<li><a href="javascript:void(0)"><i class="fa fa-line-chart"></i> 资产负债表</a></li>
							<li><a href="javascript:void(0)"><i class="fa fa-line-chart"></i> 利润表</a></li>
							<li><a href="javascript:void(0)"><i class="fa fa-line-chart"></i> 现金流量表</a></li>
						</ul>
						<div class="clearfix"></div>
						
						<br/><br/>
						<button id="closeButton" class="ladda-button ladda-button-demo btn btn-block btn-success btn-lg" data-style="expand-right">完成结账</button>	
							
					</div>
				</div>
			</div>
		</div>
		<div class="col-lg-9 animated fadeInRight">
			<div class="mail-box-header">
				<h2>验证结果</h2>
			</div>
			<div class="mail-box">

				<table class="table table-hover table-mail">
					<tbody id="tableBody">
						<tr id="trBlank" class="unread">
							<td colspan="10" style="text-align: center; font-size: 14px; font-weight: normal;"><i class="fa fa-inbox"></i> 无结果反馈<br/></td>
						</tr>
					</tbody>
				</table>


			</div>
		</div>
	</div>
</div>

<table style="display: none;">
	<tbody id="trModel">
		<tr class="unread trHintClass">
			<td width="5%" style="padding-left: 50px; vertical-align: middle;"><i class="fa fa-exclamation-triangle fa-lg text-#hintClass#"></i></td>
			<td width="85%" style="vertical-align: middle;">#hintMessage#</td>
			<td width="10%" style="padding-right: 50px; vertical-align: middle;"><button type="button" class="btn btn-outline btn-#hintClass#" onclick="window.open('#settleLink#')"><i class="fa fa-external-link"></i> 去处理</button></td>
		</tr>
	</tbody>
</table>

<script>
	$(document).ready(function() {
		var l = $('.ladda-button-demo').ladda();
	
	   	$(document).ready(function(){
	       $('.i-checks').iCheck({
	           checkboxClass: 'icheckbox_square-green',
	           radioClass: 'iradio_square-green',
	       });
        });
        
        var validateFlag = "N";
        
        $("#periodId").change(function(){
        	validateFlag = "N";
        });
        
        $("#validateButton").click(function(){
        	l.ladda('start');
        	if($("#periodId").val()==""){
        		redragonJS.alert("请先选择结算期间");
        		l.ladda('stop');
        	}else{
        		$.ajax({
					type: "post",
					url: "web/gLJeHead/validateCloseLedger",
					data: {"periodId": $("#periodId").val()},
					async: false,
					dataType: "json",
					cache: false,
					success: function(data){
						if(data!=""){
							l.ladda('stop');
							
							removeStepClass($(".step1"));
							$(".step1").addClass(setStepClass(data.step1));
							removeStepClass($(".step2"));
							$(".step2").addClass(setStepClass(data.step2));
							removeStepClass($(".step3"));
							$(".step3").addClass(setStepClass(data.step3));
							removeStepClass($(".step4"));
							$(".step4").addClass(setStepClass(data.step4));
							removeStepClass($(".step5"));
							$(".step5").addClass(setStepClass(data.step5));
							
							removeStepClass($(".step1Text"));
							$(".step1Text").addClass(setStepClass1(data.step1));
							removeStepClass($(".step2Text"));
							$(".step2Text").addClass(setStepClass1(data.step2));
							removeStepClass($(".step3Text"));
							$(".step3Text").addClass(setStepClass1(data.step3));
							removeStepClass($(".step4Text"));
							$(".step4Text").addClass(setStepClass1(data.step4));
							removeStepClass($(".step5Text"));
							$(".step5Text").addClass(setStepClass1(data.step5));
							
							$(".step1Text").text(setStepText(data.step1));
							$(".step2Text").text(setStepText(data.step2));
							$(".step3Text").text(setStepText(data.step3));
							$(".step4Text").text(setStepText(data.step4));
							$(".step5Text").text(setStepText(data.step5));
							
							//提示信息
							if(data.hints.length==0){
								$("#tableBody .trHintClass").remove();
								$("#trBlank").show();
							}else{
								$("#trBlank").hide();
								$("#tableBody .trHintClass").remove();
								
								$.each(data.hints,function(index, content){
									var trHtml = $("#trModel").clone().html().replace("#hintClass#", content.hintClass).replace("#hintClass#", content.hintClass).replace("#hintMessage#", content.hintMessage).replace("#settleLink#", content.settleLink);
									$("#tableBody").append(trHtml);
								});
							}
							
							//判断验证最终结果
							if(data.step1=="pass"&&data.step2=="pass"&&data.step3=="pass"&&data.step4=="pass"&&data.step5=="pass"){
								validateFlag = "Y";
							}
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						redragonJS.alert(textStatus);
					}
				});
        	}
        });
        
        $("#closeButton").click(function(){
        	if(validateFlag=="Y"){
        		redragonJS.confirm("确认完成结账？此操作不可逆", function(){
        			if($("#periodId").val()==""){
		        		redragonJS.alert("请先选择结算期间");
		        	}else{
		        		l.ladda('start');
						window.location.href='web/gLJeHead/closeLedger?periodId='+$("#periodId").val();
		        	}
				});
        	}else{
        		redragonJS.alert("请先完成验证后再结账");
        	}
        });
	
	});
	
 	function setStepClass(stepStatus){
    	if(stepStatus=="pass"){
    		return "text-info";
    	}else if(stepStatus=="warn"){
    		return "text-warning";
    	}if(stepStatus=="refuse"){
    		return "text-danger";
    	}else{
    		return "text-default";
    	}
    }
    
    function removeStepClass(obj){
    	$(obj).removeClass("text-info");
    	$(obj).removeClass("text-warning");
    	$(obj).removeClass("text-danger");
    	$(obj).removeClass("text-default");
    	
		$(obj).removeClass("label-info");
    	$(obj).removeClass("label-warning");
    	$(obj).removeClass("label-danger");
    	$(obj).removeClass("label-default");
    }
    
    function setStepClass1(stepStatus){
    	if(stepStatus=="pass"){
    		return "label-info";
    	}else if(stepStatus=="warn"){
    		return "label-warning";
    	}if(stepStatus=="refuse"){
    		return "label-danger";
    	}else{
    		return "label-default";
    	}
    }
    
    function setStepText(stepStatus){
    	if(stepStatus=="pass"){
    		return "通过";
    	}else if(stepStatus=="warn"){
    		return "警告";
    	}if(stepStatus=="refuse"){
    		return "不通过";
    	}else{
    		return "未验证";
    	}
    }
</script>